package com.xying.util;

import com.google.common.collect.Lists;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;
import java.util.List;

/**
 * Created by Hisen on 2017/11/9.
 */
public class DateTimeUtil {
  protected static String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
  protected static final Integer DEFAULT_MIN = 30;
  protected static final Boolean DEFAULT_GET = true;

  public static String dateToString (Date date){
    return dateToString(date, STANDARD_FORMAT);
  }

  public static String dateToString (Date date, String formatString){
    if(date == null){
      return StringUtils.EMPTY;
    }

    DateTime dateTime = new DateTime(date);
    return dateTime.toString(formatString);
  }

  public static Date stringToDate (String dateString){
    return stringToDate(dateString, STANDARD_FORMAT);
  }

  public static Date stringToDate (String dateString, String formatString){
    DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(formatString);
    DateTime dateTime = dateTimeFormat.parseDateTime(dateString);
    return dateTime.toDate();
  }

  public static List listDateInterval(Date startTime, Date endTime){
    return listDateInterval(startTime, endTime, DEFAULT_MIN);
  }

  public static List listDateInterval(Date startTime, Date endTime, Boolean isGetList){
    return listDateInterval(startTime, endTime, isGetList, DEFAULT_MIN);
  }

  public static List listDateInterval(Date startTime, Date endTime, Integer period){
    if (period == null) {
      return listDateInterval(startTime, endTime);
    }
    List<Date> dateList = Lists.newArrayList();
    while (startTime.getTime() <= endTime.getTime()){
      dateList.add(startTime);
      startTime = new Date(startTime.getTime() + period * 60 * 1000);
    }
    return dateList;
  }

  public static List listDateInterval(Date startTime, Date endTime, Boolean isGetList, Integer period){
    if (period == null) {
      return listDateInterval(startTime, endTime, isGetList);
    }

    if (isGetList.equals(DEFAULT_GET)){
      List<List<Date>> dateListList = Lists.newArrayList();
      while (startTime.getTime() <= endTime.getTime()) {
        List<Date> dateList = Lists.newArrayList();
        dateList.add(startTime);
        dateListList.add(dateList);
        startTime = new Date(startTime.getTime() + period * 60 * 1000);
      }
      return dateListList;
    }else {
      return listDateInterval(startTime, endTime, period);
    }

  }

  public static List listDateInterval(Date startTime, Date endTime, Boolean isGetList, Boolean isGetTime, Integer period){
    if (period == null) {
      return listDateInterval(startTime, endTime, isGetList);
    }

    if (isGetList.equals(DEFAULT_GET) && isGetTime.equals(DEFAULT_GET)){
      List<List<Long>> dateListList = Lists.newArrayList();
      while (startTime.getTime() <= endTime.getTime()) {
        List<Long> dateList = Lists.newArrayList();
        dateList.add(startTime.getTime());
        dateListList.add(dateList);
        startTime = new Date(startTime.getTime() + period * 60 * 1000);
      }
      return dateListList;
    }else if (isGetList.equals(DEFAULT_GET)) {
      return listDateInterval(startTime, endTime, isGetList, period);
    } else {
      return listDateInterval(startTime, endTime, period);
    }
  }

}
